﻿using System;
using System.Collections.Generic;
using System.Linq;

class LINQExamples
{
    public static IEnumerable<Student> ExtractStudentsWithFirstNameGreaterThanLast(Student[] students)
    {
        var selectedStudents = from student in students
                               where student.FirstName.CompareTo(student.LastName) < 0
                               select student;
        return selectedStudents;
    }

    public static dynamic ExtractStudentsWithCertainAge(Student[] students)
    {
        var selectedStudents = from student in students
                               where student.Age > 18 && student.Age < 24
                               select new { FirstName = student.FirstName, LastName = student.LastName };
        return selectedStudents;
    }

    public static dynamic SortStudentsDescendingLambda(Student[] students)
    {
        var sortedStudents = 
            students.OrderByDescending(x => x.FirstName).ThenByDescending(x => x.LastName);
        return sortedStudents;
    }

    public static dynamic SortStudentsDescendingLINQ(Student[] students)
    {
        var sortedStudents = from student in students orderby student.FirstName descending, 
                                 student.LastName descending select student;
        return sortedStudents;  
    }

    public static void Main()
    {
        Student[] students = new Student[3];
        Student ivan = new Student("Ivan", "Spasov", 21);
        Student valeri = new Student("Valeri", "Dobrev", 25);
        Student pesho = new Student("Ivan", "Peshev", 222);
        students[0] = ivan;
        students[1] = valeri;
        students[2] = pesho;
        var sortedLambdaStudents = SortStudentsDescendingLambda(students);
        var sortedLINQStudents = SortStudentsDescendingLINQ(students);
        foreach (var student in sortedLambdaStudents)
        {
            Console.WriteLine(student);
        }
        foreach (var student in sortedLINQStudents)
        {
            Console.WriteLine(student);
        }
    }
}
